/*
*   Grupo 2:		Ana Elisa Siena			7126563
*                       Larissa Zimmermann		7126879
* 
*   Jogo em Java - AngryBirds
*   SCC - 604 - Programação Orientada a Objetos
*   Prof. Fernando V. Paulovich
*   Engenharia de Computação 010 - USP - São Carlos
*   Segundo Semestre de 2011 
*	
*/

package angrybirds.menus;

import angrybirds.main.Angrybirds;
import java.awt.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;


/*******************************************************************************
 *	Nome da classe:     MenuInicial
 *	O que faz:          Inicializa o menu          		
 *******************************************************************************/
public class MenuInicial extends BasicGameState {

    private int stateID = -1;
    private Image background = null;
    private Image iniciarJogo = null;
    private Image sairJogo = null;
    private float iniciarJogoEscala = 1;
    private float sairJogoEscala = 1;
    private Music musicaFundo = null;
    private Sound musicaOpcao = null;
    private static final float menuX = 260;
    private static final float menuY = 250;

    public MenuInicial(int stateID) throws SlickException {
        this.stateID = stateID;
    }

    @Override
    public int getID() {
        return this.stateID;
    }

    public void setStateID(int stateID) throws SlickException {
        this.stateID = stateID;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sb) throws SlickException {

        this.background = new Image("imagensJogo/FundoMenu.jpg");

        // carrega os botões do menu
        this.iniciarJogo = new Image("imagensJogo/BotaoPlayJogo.png");
        this.sairJogo = new Image("imagensJogo/BotaoSairJogo.png");

        Font font = new Font("Verdana", Font.BOLD, 20);
        musicaFundo = new Music("sons/AngryBirdsTheme.wav");
        musicaOpcao = new Sound("sons/SelecaoBotoes.wav");
    }
    
    private float menuExitY;

    @Override
    public void render(GameContainer gc, StateBasedGame sb, Graphics grphcs) {
        //Desenha tudo

        //Desenha Background
        this.background.draw(0, 0);
        
        //Desenha Botões
        this.iniciarJogo.draw(this.menuX, this.menuY, this.iniciarJogoEscala);    
        this.menuExitY = this.menuY + this.iniciarJogo.getHeight();
        this.sairJogo.draw(this.menuX, this.menuExitY, this.sairJogoEscala);
    }
    
    private float escala = 0.001f;

    @Override
    public void update(GameContainer gc, StateBasedGame sb, int delta) {

        Input input = gc.getInput();//Detector de entrada: mouse, teclado e tudo mais....

        int mouseX = input.getMouseX();
        int mouseY = input.getMouseY();
        //Detecta onde foi o click do mouse...em que posição!!

        boolean insideRecordsGame = false;
        boolean insideStartGame = false;//estado se o mouse coincide coms os pontos do play game
        boolean insideExit = false;//estado se o mouse coincide coms os pontos do exit game
        //Estados: false -> nao coincide; true -> coindide;

        this.menuExitY = this.menuY + this.iniciarJogo.getHeight();
        
        if(!musicaFundo.playing())musicaFundo.play();

        //Verifica se o mouse passou em cima das opções do menu.
        //OPÇÃO PLAY....
        if ((mouseX >= this.menuX && mouseX <= (this.menuX + this.iniciarJogo.getWidth()))
                && (mouseY >= this.menuY && mouseY <= this.menuY + this.iniciarJogo.getHeight())) {
            insideStartGame = true;
        } else if ((mouseX >= this.menuX && mouseX <= (this.menuX + this.sairJogo.getWidth()))//RECORDS
                && (mouseY >= this.menuExitY && mouseY <= this.menuExitY + this.sairJogo.getHeight())) {
            insideExit = true;
        } else if ((mouseX >= this.menuX && mouseX <= (this.menuX + this.sairJogo.getWidth()))//SAIR
                && (mouseY >= this.menuExitY && mouseY <= this.menuExitY + this.sairJogo.getHeight())) {
            insideExit = true;
        }

        //Se insideStartGame == true for verdadeiro, então suba o botão -> indicação que o mouse está em cima
        //VEJA TBM SE OCORRER O CASO DE CLICAR, SELECIONAR UMA OPÇÃO

        if (insideStartGame) {
            if (this.iniciarJogoEscala < 1.05f) {
                this.iniciarJogoEscala += this.escala * delta;
                musicaOpcao.play();
            }
            if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                sb.enterState(Angrybirds.CHOOSEGAMEPLAYSTATE);
                this.musicaFundo.stop();
            }
        } else {
            if (this.iniciarJogoEscala > 1.0f) {
                this.iniciarJogoEscala -= this.escala * delta;
            }
        }

        if (insideExit) {
            if (this.sairJogoEscala < 1.05f) {
                this.sairJogoEscala += this.escala * delta;
                musicaOpcao.play();
            }
            if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
                gc.exit();
            }
        } else {
            if (this.sairJogoEscala > 1.0f) {
                this.sairJogoEscala -= this.escala * delta;
            }
        }
    }
}
